package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPersonalAnalysis

import android.R.attr.*
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperPerAnalysisInfor
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import kotlinx.android.synthetic.main.aibk_adapter_answersheet_child.view.*


class AibkAnalysisAnswerSheetChildAdapter(
        val activity: AppCompatActivity,
        val paperPerAnalysisInfor: PaperPerAnalysisInfor,
        var index_A: Int
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_answersheet_child, parent, false)
    }

    override fun onBindViewHolder(
            holder: KBaseRecyclerViewHolder,
            position: Int
    ) {
        var index_B = position_return_index_B(paperPerAnalysisInfor, position)
        var index_C = position_return_index_C(paperPerAnalysisInfor, position)

        var sortIndex = paperPerAnalysisInfor.TypeList[index_A].QuesList[index_B]
                .SortIndexList[index_C].SortIndex
        var scoreRate = paperPerAnalysisInfor.TypeList[index_A].QuesList[index_B]
                .SortIndexList[index_C].ScoreRate
        var state = paperPerAnalysisInfor.TypeList[index_A].QuesList[index_B]
                .SortIndexList[index_C].State
        holder.convertView.BtnSortIndex.text = sortIndex

        if (sortIndex.contains("-")) {
            val linearParams = holder.convertView.BtnSortIndex.layoutParams
            linearParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
            linearParams.height = DensityUtils.dp2px(30f)
            holder.convertView.BtnSortIndex.layoutParams = linearParams
            if (state == 0) {
                holder.convertView.BtnSortIndex.setBackgroundResource(R.drawable.aibk_drawable_button_blue_gct_radius_50)
            } else {
                if (scoreRate >= 0.6) {
                    holder.convertView.BtnSortIndex.setBackgroundResource(R.drawable.aibk_drawable_button_green_gct_radius_50)
                } else {
                    holder.convertView.BtnSortIndex.setBackgroundResource(R.drawable.aibk_drawable_button_red_gct_radius_50)
                }
            }
        } else {
            val linearParams = holder.convertView.BtnSortIndex.layoutParams
            linearParams.width = DensityUtils.dp2px(30f)
            linearParams.height = DensityUtils.dp2px(30f)
            holder.convertView.BtnSortIndex.layoutParams = linearParams
            if (state == 0) {
                holder.convertView.BtnSortIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_blue)
            } else {
                if (scoreRate >= 0.6) {
                    holder.convertView.BtnSortIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_green)
                } else {
                    holder.convertView.BtnSortIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_red)
                }
            }
        }


        holder.convertView.BtnSortIndex.setTextColor(activity.resources.getColor(R.color.lancoooWhite))

        holder.convertView.BtnSortIndex.setOnClickListener {
            var index_C = position_return_index_C(paperPerAnalysisInfor, position)
            clickChilidListener!!.onClick(index_A, index_B, index_C)
        }

    }

    override fun getItemCount(): Int {
        var itemCount = 0
        var quesListSize = paperPerAnalysisInfor.TypeList[index_A].QuesList.size
        for (b in 0 until quesListSize) {
            itemCount += paperPerAnalysisInfor.TypeList[index_A].QuesList[b].SortIndexList.size
        }
        return itemCount
    }

    interface OnClickChildListener {
        fun onClick(index_A: Int, index_B: Int, index_C: Int)
    }

    private var clickChilidListener: OnClickChildListener? = null

    fun setClickChildListener(clickChilidListener: OnClickChildListener?) {
        this.clickChilidListener = clickChilidListener
    }

    private fun position_return_index_B(paperPerAnalysisInfor: PaperPerAnalysisInfor, position: Int): Int {
        var postionX = -1
        var index_B = 0
        var quesListSize = paperPerAnalysisInfor.TypeList[index_A].QuesList.size
        outLoop@ for (b in 0 until quesListSize) {
            var sortIndexListSize = paperPerAnalysisInfor.TypeList[index_A].QuesList[b]
                    .SortIndexList.size
            for (c in 0 until sortIndexListSize) {
                postionX += 1
                if (position == postionX) {
                    index_B = b
                    break@outLoop
                }
            }
        }
        return index_B
    }

    private fun position_return_index_C(paperPerAnalysisInfor: PaperPerAnalysisInfor, position: Int): Int {
        var postionX = -1
        var index_C = 0
        var quesListSize = paperPerAnalysisInfor.TypeList[index_A].QuesList.size
        outLoop@ for (b in 0 until quesListSize) {
            var sortIndexListSize = paperPerAnalysisInfor.TypeList[index_A].QuesList[b]
                    .SortIndexList.size
            for (c in 0 until sortIndexListSize) {
                postionX += 1
                if (position == postionX) {
                    index_C = c
                    break@outLoop
                }
            }
        }
        return index_C
    }

}